package cn.lingyangwl.agile.auth.controller;

import cn.lingyangwl.agile.auth.model.rqrs.*;
import cn.lingyangwl.framework.core.response.Resp;
import cn.lingyangwl.agile.auth.model.entity.LocalUser;
import cn.lingyangwl.agile.auth.service.LocalUserService;
import cn.lingyangwl.agile.common.security.annotation.InnerAuth;
import cn.lingyangwl.agile.ram.ActionType;
import cn.lingyangwl.agile.ram.RamResource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author shenguangyang
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/localUser")
public class AuthUserController {
    @Resource
    private LocalUserService localUserService;

    /**
     * 更新授权信息(不会更新密码)
     */
    @InnerAuth
    @PostMapping("/updateLocalUser")
    public Resp<?> updateLocalUser(@RequestBody @Validated LocalUser req) {
        localUserService.updateLocalUser(req);
        return Resp.ok();
    }

    /**
     * 批量更新用户授权信息
     */
    @InnerAuth
    @PostMapping(value = "/batchUpdateLocalUser")
    public Resp<?> batchUpdateLocalUser(@RequestBody List<LocalUser> reqList) {
        localUserService.batchUpdateLocalUser(reqList);
        return Resp.ok();
    }

    /**
     * 保存用户授权信息
     */
    @InnerAuth
    @PostMapping(value = "/save")
    public Resp<Long> saveLocalUser(@RequestBody @Validated LocalUser req) {
        localUserService.saveLocalUser(req);
        return Resp.ok(req.getAuthId());
    }

    @InnerAuth
    @GetMapping(value = "/get")
    public Resp<LocalUser> getUser(@Validated LocalUserGetReq rq) {
        LocalUser localUser = localUserService.getUser(rq);
        return Resp.ok(localUser);
    }

    /**
     * 删除用户授权信息
     */
    @InnerAuth
    @DeleteMapping(value = "/delete")
    public Resp<?> deleteLocalUser(@RequestParam("userId") Long userId) {
        localUserService.deleteLocalUser(userId);
        return Resp.ok();
    }

    /**
     * 删除用户授权信息
     */
    @InnerAuth
    @DeleteMapping(value = "/batchDelete")
    public Resp<?> batchDeleteLocalUser(@RequestParam("tenantId") String tenantId, @RequestParam("userIds") List<Long> userIds) {
        localUserService.batchDeleteLocalUser(tenantId, userIds);
        return Resp.ok();
    }

    /**
     * 用户重置密码, 只有当前登录账号采用权限重置密码
     */
    @PostMapping(value = "/userResetPassword")
    public Resp<?> userResetPassword(@RequestBody @Validated UserResetPwdReq rq) {
        localUserService.resetPassword(rq);
        return Resp.ok();
    }

    /**
     * 管理员重置密码(重置任何人的, 权力较大, 请谨慎分配权限)
     */
    @RamResource(action = "auth:localUser:resetUserPassword", name = "管理员重置密码", type = ActionType.WRITE)
    @PostMapping("/adminResetPassword")
    public Resp<?> changePassword(@RequestBody @Validated AdminResetPwdReq rq) {
        if (!localUserService.resetPassword(rq)) {
            return Resp.fail("密码修改失败, 请联系管理员!!");
        }
        return Resp.ok();
    }

    /**
     * 查询用户授权信息
     */
    @GetMapping(value = "/listAuth")
    @RamResource(action = "auth:localUser:list", name = "查询用户授权列表", type = ActionType.READ)
    public Resp<List<LocalUser>> listAuth(LocalUserListReq query) {
        List<LocalUser> resp = localUserService.listAuth(query);
        return Resp.ok(resp);
    }

    @InnerAuth
    @GetMapping(value = "/getUserByUserId")
    public Resp<LocalUser> getUserByUserId(@RequestParam("userId") Long userId) {
        return Resp.ok(localUserService.getUserByUserId(userId));
    }

    /**
     * 注销账号
     */
    @PostMapping(value = "/cancelAccount")
    public Resp<?> cancelAccount(@RequestBody @Validated CancelAccountReq req) {
        localUserService.cancelAccount(req);
        return Resp.ok();
    }
}
